ecodeSDK.load({
  id: 'c5d4d4032d6340dfbe3baa982d5be19e',
  noCss: true,
  cb: function () {
  }
});

/**
 * 权重控制
 * 明细表中的指标类型为一样的行，权重合计不得大于100
 * 2021-1-14
 * ydh
 */
var zbdm_dt1 = WfForm.convertFieldNameToId("zbdm","detail_1"); //指标代码
var qz_dt1 = WfForm.convertFieldNameToId("qz","detail_1");//权重
var xmlb_dt1 = WfForm.convertFieldNameToId("xmlb","detail_1");//项目类别
var zbdm_dt2 = WfForm.convertFieldNameToId("zbdm","detail_2"); //指标代码
var qz_dt2 = WfForm.convertFieldNameToId("qz","detail_2");//权重
var xmlb_dt2 = WfForm.convertFieldNameToId("xmlb","detail_2");//项目类别
var zbdm_dt3 = WfForm.convertFieldNameToId("zbdm","detail_3"); //指标代码
var qz_dt3 = WfForm.convertFieldNameToId("qz","detail_3");//权重
var xmlb_dt3 = WfForm.convertFieldNameToId("xmlb","detail_3");//项目类别
var zbdm_dt4 = WfForm.convertFieldNameToId("zbdm","detail_4"); //指标代码
var qz_dt4 = WfForm.convertFieldNameToId("qz","detail_4");//权重
var xmlb_dt4 = WfForm.convertFieldNameToId("xmlb","detail_4");//项目类别
var zbdm_dt5 = WfForm.convertFieldNameToId("zbdm","detail_5"); //指标代码
var qz_dt5 = WfForm.convertFieldNameToId("qz","detail_5");//权重
var xmlb_dt5 = WfForm.convertFieldNameToId("xmlb","detail_5");//项目类别
var zbdm_dt6 = WfForm.convertFieldNameToId("zbdm","detail_6"); //指标代码
var qz_dt6 = WfForm.convertFieldNameToId("qz","detail_6");//权重
var xmlb_dt6 = WfForm.convertFieldNameToId("xmlb","detail_6");//项目类别
var zbdm_dt7 = WfForm.convertFieldNameToId("zbdm","detail_7"); //指标代码
var qz_dt7 = WfForm.convertFieldNameToId("qz","detail_7");//权重
var xmlb_dt7 = WfForm.convertFieldNameToId("xmlb","detail_7");//项目类别
var map = new Map;// 指标代码 - 权重Map
jQuery(function() {
	checkCustomize = function(){
	  var mark = true;
        // 明细1
		var detailMark_1 = WfForm.getDetailAllRowIndexStr("detail_1");
		var index_1 = new Array(); 
		index_1 = detailMark_1.split(","); 
		for ( var i = 0; i < index_1.length; i++) {
			var index = i;
			var xmlbValue =  WfForm.getBrowserShowName(xmlb_dt1+'_'+index).trim();
			var qzValue = WfForm.getFieldValue(qz_dt1+'_'+index);
			if(!valueIsNull(xmlbValue)){
				setWeightLimit(xmlbValue,qzValue);
			}
		}
		// 明细2
		var detailMark_2 = WfForm.getDetailAllRowIndexStr("detail_2");
		var index_2 = new Array(); 
		index_2 = detailMark_2.split(","); 
		for ( var i = 0; i < index_2.length; i++) {
			var index = i;
			var xmlbValue =  WfForm.getBrowserShowName(xmlb_dt2+'_'+index).trim();
			var qzValue = WfForm.getFieldValue(qz_dt2+'_'+index);
			if(!valueIsNull(xmlbValue)){
				setWeightLimit(xmlbValue,qzValue);
			}
		}
		// 明细3
		var detailMark_3 = WfForm.getDetailAllRowIndexStr("detail_3");
		var index_3 = new Array(); 
		index_3 = detailMark_3.split(","); 
		for ( var i = 0; i < index_3.length; i++) {
			var index = i;
			var xmlbValue =  WfForm.getBrowserShowName(xmlb_dt3+'_'+index).trim();
			var qzValue = WfForm.getFieldValue(qz_dt3+'_'+index);
			if(!valueIsNull(xmlbValue)){
				setWeightLimit(xmlbValue,qzValue);
			}
		}
		// 明细4
		var detailMark_4 = WfForm.getDetailAllRowIndexStr("detail_4");
		var index_4 = new Array(); 
		index_4 = detailMark_4.split(","); 
		for ( var i = 0; i < index_4.length; i++) {
			var index = i;
			var xmlbValue =  WfForm.getBrowserShowName(xmlb_dt4+'_'+index).trim();
			var qzValue = WfForm.getFieldValue(qz_dt4+'_'+index);
			if(!valueIsNull(xmlbValue)){
				setWeightLimit(xmlbValue,qzValue);
			}
		}
		// 明细5
		var detailMark_5 = WfForm.getDetailAllRowIndexStr("detail_5");
		var index_5 = new Array(); 
		index_5 = detailMark_5.split(","); 
		for ( var i = 0; i < index_5.length; i++) {
			var index = i;
			var xmlbValue =  WfForm.getBrowserShowName(xmlb_dt5+'_'+index).trim();
			var qzValue = WfForm.getFieldValue(qz_dt5+'_'+index);
			if(!valueIsNull(xmlbValue)){
				setWeightLimit(xmlbValue,qzValue);
			}
		}
		// 明细6
		var detailMark_6 = WfForm.getDetailAllRowIndexStr("detail_6");
		var index_6 = new Array(); 
		index_6 = detailMark_6.split(","); 
		for ( var i = 0; i < index_6.length; i++) {
			var index = i;
			var xmlbValue =  WfForm.getBrowserShowName(xmlb_dt6+'_'+index).trim();
			var qzValue = WfForm.getFieldValue(qz_dt6+'_'+index);
			if(!valueIsNull(xmlbValue)){
				setWeightLimit(xmlbValue,qzValue);
			}
		}
		// 明细7
		var detailMark_7 = WfForm.getDetailAllRowIndexStr("detail_7");
		var index_7 = new Array(); 
		index_7 = detailMark_7.split(","); 
		for ( var i = 0; i < index_7.length; i++) {
			var index = i;
			var xmlbValue =  WfForm.getBrowserShowName(xmlb_dt7+'_'+index).trim();
			var qzValue = WfForm.getFieldValue(qz_dt7+'_'+index);
			if(!valueIsNull(xmlbValue)){
				setWeightLimit(xmlbValue,qzValue);
			}
		}
	  
		// 判断相同指标代码的权重是否超过100
		map.forEach(function(value,key){
			console.log(key + ":" + value);
			if(value != 100){
				alert("项目类别为"+key+"的权重必须为100，现在为"+value+"请重新填写！");
				mark = false;
			}
		})
		map = new Map;
    return mark;
    };
});

/**
 * 权重map 
 * @param xmlbValue 项目类别
 * @param qzValue 权重
 * @returns
 */
function setWeightLimit(xmlbValue,qzValue){
	var value = map.get(xmlbValue);
	if(!valueIsNull(value)){
		value = value*1 + qzValue*1;
	} else {
		value = qzValue;
	}
	map.set(xmlbValue,value);
}

/**
 * 判断数据是否为Null或者undefined或者为空字符串
 * @param value
 * @returns {boolean}
 * @constructor
 */
function valueIsNull(obj) {
    if (null == obj || "undefined" == typeof (obj) || "" == obj) {
        return true;
    }
    if (Array.isArray(obj)) {
        if (obj.length > 0) {
            return false;
        } else {
            return true;
        }
    } else {
        var result = obj;
        if ("" == result) {
            return true;
        } else {
            return false;
        }
    }
}